package com.lancoo.answer.view.fragment.composition.continuation.sub;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;

import java.util.List;

/**
 * Created by pengmi on 2022/5/26 14:20
 * Description:读后续写图片作答
 */
public class ContinuationPictureAnswerFragment extends Fragment {


    private Item item;

    public static Fragment loadFragment(int typeIndex, int queseIndex, int childIndex){

        ContinuationPictureAnswerFragment fragment=new ContinuationPictureAnswerFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX,typeIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX,queseIndex);
        mBundle.putInt(AnswerConstant.KEY_CHILDINDEX,childIndex);
        fragment.setArguments(mBundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ev_fragment_composition_continuation_audio_answer_layout, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(getArguments()==null){
            return;
        }
        int typeIndex = getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        int queseIndex = getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        int childIndex = getArguments().getInt(AnswerConstant.KEY_CHILDINDEX);
        if(ConstantBean.Companion.getConstantExamBean()==null){
            return;
        }
        List<Type> typeList = ConstantBean.Companion.getConstantExamBean().getTypeList();
        if(typeList==null||typeIndex>=typeList.size()){
            return;
        }
        Type type = typeList.get(typeIndex);
        List<Ques> quesList = type.getQuesList();
        if(quesList==null||quesList.size()<=queseIndex){
            return;
        }
        Ques ques = quesList.get(queseIndex);
        List<Child> childList = ques.getChildList();
        if(childList==null||childList.size()<=childIndex){
            return;
        }
        Child child = childList.get(childIndex);
        List<Item> itemList = child.getItemList();
        if(itemList==null||itemList.size()==0){
            return;
        }
        item = itemList.get(0);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        ContinuationImageAnswerAdapter mAdapter=new ContinuationImageAnswerAdapter(item,typeIndex,queseIndex,childIndex);
        recyclerView.setAdapter(mAdapter);
    }
}
